﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace LocalOverride.Fiddler.Store
{
    public class MappingEntryValidator : IValidator
    {
        public MappingEntryValidator(MappingEntry mappingEntry, ILocalFileAccess localFileAccess)
        {
            validators = new List<IValidator>
                             {
                                 new UrlValidator(mappingEntry),
                                 new LocalPathValidator(mappingEntry, localFileAccess)
                             };
        }

        private readonly IEnumerable<IValidator> validators;

        public ValidationResult Validate()
        {

            bool isValid = true;
            var errorsFound = new List<ErrorNotification>();

            var failingValidators = validators.Select(x => x.Validate()).Where(result => !result.IsValid);
            foreach (var validator in failingValidators)
            {
                isValid = false;
                errorsFound.AddRange(validator.Errors);
            }

            return new ValidationResult(isValid, errorsFound);
        }
    }
}